//
//  UUFleetManager.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 21/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUFleetManager.h"
#import "UUFleetUtility.h"
#import <UUFoundation/UUIncrementDataManager.h>
#import <UUFoundation/DKDataManager.h>
#import "UUFleetRequestModel.h"
#import "UUFleetAddRequestModel.h"
#import "UUFleetEditRequestModel.h"
#import "UUFleetDeleteRequestModel.h"

@interface UUFleetManager ()

@property (nonatomic, strong) UUIncrementDataManager *fleetDataManager;

@end

@implementation UUFleetManager

- (instancetype)init {
    self = [super init];
    if (self) {
        self.fleetDataManager = [UUIncrementDataManager managerWithConfiguration:
                                 [UUIncrementDataManagerConfiguration configurationWithRequestModelClass:[UUFleetRequestModel class]
                                                                                          storeContainer:[UUFleetUtility fleetDefaults]]];
    }
    return self;
}

- (void)asyncUpdateFleets {
    WEAK_SELF();
    [self.fleetDataManager asyncIncrementUpdateWithCompleteBlock:^(BOOL hadUpdate, NSArray<id> *insert, NSArray<id> *delete, NSError *error) {
        if (hadUpdate) {
            if (insert.count > 0) {
                [weakSelf notifyObserversWithSelector:@selector(fleetManagerDidUpdateFleets) withObject:nil];
            }
            
            if (delete.count > 0) {
                [weakSelf notifyObserversWithSelector:@selector(fleetManagerDidDeleteFleets:) withObject:delete];
            }
        }
    }];
}

- (NSArray<NSNumber *> *)fetchFleets {
    NSArray<NSNumber *> *fleets = [UUFleetModel searchColumn:@"fleetId" where:nil orderBy:@"updateTime desc" offset:0 count:0];
    return fleets;
}

- (UUFleetModel *)fetchFleetWithFleetId:(id)fleetId {
    return (UUFleetModel *)[self.fleetDataManager fetchModelWithPrimaryKeyValue:fleetId];
}

- (void)asyncDeleteFleetWithFleetId:(NSNumber *)fleetId completeBlock:(void (^)(NSError *error))completeBlock {
    UUFleetDeleteRequestModel *requestModel = [UUFleetDeleteRequestModel new];
    requestModel.fleetId = fleetId;
    
    WEAK_SELF();
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            [weakSelf asyncUpdateFleets];
        }
        
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

- (void)addFleetWithDistrictIds:(NSString *)districtIds
                           name:(NSString *)name
                        contact:(NSString *)contact
                  contactMobile:(NSString *)contactMobile
                  completeBlock:(void (^)(NSError *error))completeBlock {
    UUFleetAddRequestModel *requestModel = [UUFleetAddRequestModel new];
    requestModel.districtIds = districtIds;
    requestModel.name = name;
    requestModel.contact = contact;
    requestModel.contactMobile = contactMobile;
    
    WEAK_SELF();
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            [weakSelf asyncUpdateFleets];
        }
        
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

- (void)updateFleetWithFleetId:(id)fleetId
                   districtIds:(NSString *)districtIds
                          name:(NSString *)name
                       contact:(NSString *)contact
                 contactMobile:(NSString *)contactMobile
                 completeBlock:(void (^)(NSError *error))completeBlock {
    UUFleetEditRequestModel *requestModel = [UUFleetEditRequestModel new];
    requestModel.fleetId = fleetId;
    requestModel.districtIds = districtIds;
    requestModel.name = name;
    requestModel.contact = contact;
    requestModel.contactMobile = contactMobile;
    
    WEAK_SELF();
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            [weakSelf asyncUpdateFleets];
        }
        
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

@end
